﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;

public partial class admin_LocationDetail : SitePage
{
    #region Fields
    busLocation _busLocation = new busLocation();

    public int LocationId
    {
        get
        {
            object o = ViewState["LocationId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }
        set { ViewState["LocationId"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }



    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private void LoadData()
    {
        if (this.LocationId > 0)
        {

            Location objLocation = _busLocation.GetByID(this.LocationId);
            if (objLocation != null)
            {
                txtName.Text = objLocation.Name.Trim();
                if (objLocation.IsActive)
                    chkIsActive.Checked = true;
                else chkIsActive.Checked = false;
            }
        }
    }
    private void InitData()
    {
        string validateGroup = "logoDetail";
        vsumForm.ValidationGroup = btnSave.ValidationGroup = reqName.ValidationGroup = validateGroup;
        //Init message
        reqName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Name");
        this.LocationId = clsQueryString.GetInt("Id");
    }

    private void UpdateLocation(Location objData)
    {

        objData.LocationId = this.LocationId;
        if (_busLocation.Update(objData))
            Response.Redirect("LocationList.aspx?" + this.GetPageQuery);
        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertLocation(Location objData)
    {

        if (_busLocation.Insert(objData))
            Response.Redirect("LocationList.aspx?" + this.GetPageQuery);
        else
        {

            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
        }
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get datavalue
            Location objData = new Location();
            objData.CreatedBy = this.userloginName;
            objData.CreatedDate = clsDateTime.Now;
            objData.Name = txtName.Text.Trim();
            if (chkIsActive.Checked)
                objData.IsActive = true;
            else
                objData.IsActive = false;
            objData.ModifiedBy = this.userloginName;
            objData.ModifiedDate = clsDateTime.Now;

            if (this.LocationId > 0)
                UpdateLocation(objData);
            else
                InsertLocation(objData);
        }
        else
        {
            vsumForm.ShowMessageBox = false;
            vsumForm.ShowSummary = true;
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string para = this.GetPageQuery;
        Response.Redirect("LocationList.aspx?" + para);
    }


    #endregion


}